﻿using Brc_PaaS.Api.Common;
using Brc_PaaS.Bll.Exceptionless;
using Brc_PaaS.Common.Entity;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Common.Model.Exceptionless;
using Brc_PaaS.Core.Redis;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Brc_PaaS.Api.PaaSController
{
    /// <summary>
    /// 日志业务控制器
    /// </summary>
    [Route("PaaS/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName = "PaaSExceptionless")]
    public class PaaSExceptionlessController : AdminBaseController
    {
        private IExceptionlessLog _exceptionless;
        string token;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="exceptionless"></param>
        public PaaSExceptionlessController(IConfiguration configuration, IExceptionlessLog exceptionless) : base(configuration)
        {
            _exceptionless = exceptionless;
            using (IRedisManager redisManager = new RedisManager(RedisServerEntity))
            {
                 token = redisManager.GetOrFetchString<TokenModel>("ExceptionlessTokenFresh", x =>
                {
                    return _exceptionless.Login(new EplLoginModel()
                    {
                        AppKey = configuration.GetValue<string>("Exceptionless:ApiKey"),
                        Email = configuration.GetValue<string>("Exceptionless:UserName"),
                        Password = configuration.GetValue<string>("Exceptionless:PassWords")
                    })?.Data;
                },TimeSpan.FromHours(5))?.Token;
            }
        }

        /// <summary>
        /// 创建项目
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<BaseResponseModel> Create(Bas_System model)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            var accountId = "638062f50a9f4892adfdb7fb78979fc1";
            result = _exceptionless.CreateProject(model, accountId, token);
            return Ok(result);
        }
        /// <summary>
        /// 查询日志
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult QueryEvents(EplSearchEventReqModel model)
        {
            int total = 0;
            ResponseModel<List<EplSearchEventResModel>> result = new ResponseModel<List<EplSearchEventResModel>>() { IsSuccess = false, Msg = string.Empty, Data = new List<EplSearchEventResModel>() };
            if (model.page <= 0) model.page = 1;
            if (model.limit <= 0) model.limit = 10;
            result = _exceptionless.QueryEvents(model, out total, token);
            return Ok(new { result, total });
        }
        /// <summary>
        /// 获取日志详情
        /// </summary>
        /// <param name="eventId"></param>
        /// <returns></returns>
        [HttpGet]

        public ActionResult<ResponseModel<EplSearchExceptionDetailModel>> GetEventDetail([Required(ErrorMessage = "日志Id不能为空")]string eventId)
        {
            ResponseModel<EplSearchExceptionDetailModel> result = new ResponseModel<EplSearchExceptionDetailModel>() { IsSuccess = false, Msg = string.Empty, Data = new EplSearchExceptionDetailModel() };
          
            result = _exceptionless.GetEventDetail(eventId, token);
            return Ok(result);
        }
        /// <summary>
        /// 禁用日志系统
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]

        public ActionResult<BaseResponseModel> DisableExceptionless([Required(ErrorMessage = "日志系统Id不能为空")]string id)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            var model = _exceptionless.DisableExceptionless(id);
            result.IsSuccess = model.IsSuccess;
            result.Msg = result.Msg;
            if (model.IsSuccess)
            {
                using (var redisManager = new RedisManager(RedisServerEntity))
                {
                    redisManager.KeyDelete($"Exceptionless:{model.Data.SystemId}");
                }
            }
            return Ok(result);
        }
    }
}